﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using UsingCompositeCommands.Core;
using UsingEventAggregator.Core;

namespace ShowMsgModule.ViewModels
{
    public class ShowMsgViewModel : BindableBase
    {
        IEventAggregator _ea;
        private readonly IRegionManager _regionManager;

        private ObservableCollection<string> _messages = new ObservableCollection<string>();
        public ObservableCollection<string> Messages
        {
            get { return _messages; }
            set { SetProperty(ref _messages, value); }
        }

        private IAppCompositeCommands _appCommands;
        public IAppCompositeCommands AppCommands
        {
            get { return _appCommands; }
            set { SetProperty(ref _appCommands, value); }
        }

        public ShowMsgViewModel(IRegionManager regionManager, IEventAggregator ea, IAppCompositeCommands appComComm)
        {
            _ea = ea;
            //订阅信息
            _ea.GetEvent<MessageSentEvent>().Subscribe(MessageReceived);
            _ea.GetEvent<ChangeLanguageEvent>().Subscribe(ChangeLanguageEventSub);

            //_ea.GetEvent<PubSubEvent<string>>().Subscribe(MessageReceived);
            // 过滤
            //_ea.GetEvent<MessageSentEvent>().Subscribe(MessageReceived, 
            //    ThreadOption.PublisherThread, 
            //    false, 
            //    (filter) => filter.Contains("Brian"));

            _regionManager = regionManager;
            NavigateCommand = new DelegateCommand<string>(Navigate);
            AppCommands = appComComm;

            ChangeStyleCommand = new DelegateCommand<ResourceDictionary>(ChangeStyleSub);
            UILoadedCommand = new DelegateCommand<ResourceDictionary>(UILoadedCommand_Sub);
        }
        public DelegateCommand<ResourceDictionary> UILoadedCommand { get; private set; }
        private ResourceDictionary UIL10N;
        private void UILoadedCommand_Sub(ResourceDictionary obj)
        {
            UIL10N = obj;
            ChangeLanguageEventSub(Thread.CurrentThread.CurrentCulture.Name);
        }

        private void ChangeLanguageEventSub(string language)
        {
            string AsmName = typeof(ShowMsgViewModel).Assembly.GetName().Name;
            Uri langUri = new Uri(@"pack://application:,,,/" + AsmName + ";component/Localization/" + language + ".xaml", UriKind.RelativeOrAbsolute);
            ResourceDictionary newDictionary = new ResourceDictionary();
            newDictionary.Source = langUri;
            if (UIL10N != null)
                UIL10N.MergedDictionaries[0] = newDictionary;
        }

        private void MessageReceived(string str)
        {
            Messages.Add(str);
        }
        public DelegateCommand<string> NavigateCommand { get; private set; }
        public DelegateCommand<ResourceDictionary> ChangeStyleCommand { get; set; }
        private bool HiStyle = false;
        private void ChangeStyleSub(ResourceDictionary uc)
        {
            ResourceDictionary newDictionary = new ResourceDictionary();
            Uri HiButton = new Uri(@"pack://application:,,,/MVVMStyle;component/HiButton.xaml", UriKind.RelativeOrAbsolute);
            Uri Button = new Uri(@"pack://application:,,,/MVVMStyle;component/Button.xaml", UriKind.RelativeOrAbsolute);
            if (HiStyle)
                newDictionary.Source = Button;
            else
                newDictionary.Source = HiButton;

            HiStyle = !HiStyle;
            uc.MergedDictionaries[1] = newDictionary;
        }

        private void Navigate(string navigatePath)
        {
            if (navigatePath != null)
                _regionManager.RequestNavigate("NavigationRegion", navigatePath, NavigationComplete);

            //带参数
            //var parameters = new NavigationParameters();
            //parameters.Add("person", person);
            //_regionManager.RequestNavigate("PersonDetailsRegion", "PersonDetail", parameters);
        }

        private void NavigationComplete(NavigationResult result)
        {
            //Console.WriteLine(String.Format("Navigation to {0} complete. ", result.Context.Uri));
        }

    }
}
